//
//  QQMusicOperationTool.swift
//  QQ音乐
//
//  Created by 曹均华 on 16/6/4.
//  Copyright © 2016年 曹均华. All rights reserved.









import UIKit
import MediaPlayer

class QQMusicOperationTool: NSObject {

    
    var lastRow: Int = -1
    
    var artwork: MPMediaItemArtwork?
    
    
    
    private let musicMessageModel: QQMusicMessageModel = QQMusicMessageModel()
    
    func getNewMusicMessage() -> QQMusicMessageModel  {
        
        // 给属性赋值
        // 相当于, 保证数据的最新状态
        musicMessageModel.musicM = playMusicList?[currentPlayIndex]
        
        musicMessageModel.costTime = playTool.player?.currentTime ?? 0
        musicMessageModel.totalTime = playTool.player?.duration ?? 0
        
        musicMessageModel.isPlaying = playTool.player?.playing ?? false

        
        return musicMessageModel
        
    }
    
    
    
    
    // 单例的好处
    // 节省内存
    // 共享数据
    static let shareInstance = QQMusicOperationTool()
    
    // 记录着,当前播放的播放列表
    var playMusicList: [QQMusicModel]?
    
    // 记录当前正在播放哪一首索引
    
    var currentPlayIndex: Int = -1 {
        
        didSet {
            
            if playMusicList?.count > 0 {
                if currentPlayIndex < 0 {
                    currentPlayIndex = (playMusicList?.count)! - 1
                } else if currentPlayIndex > (playMusicList?.count)! - 1
                {
                    currentPlayIndex = 0
                }

            }
        }
        
    }
  
    
    // 真正去播放歌曲, 控制单首歌曲操作的工具类
    let playTool: QQMusicTool = QQMusicTool()
    
    
    // 下一首歌曲
    func nextMusic() -> () {
        
        // 修改当前播放歌曲索引
        currentPlayIndex += 1
        
        // 根据索引, 从当前播放列表里面, 取出需要播放的数据模型
        guard let model = playMusicList?[currentPlayIndex] else {
            return
        }
        
        // 调用方法,开始播放
        playMusic(model)
    }
    
    // 上一首
    func preMusic() -> () {
        
        // 修改当前播放歌曲索引
        currentPlayIndex -= 1
        
        // 取出需要播放的数据模型
        guard let model = playMusicList?[currentPlayIndex] else {
            return
        }
        
        // 调用方法,开始播放
        playMusic(model)
    }
    

    // 根据一个音乐数据模型, 去播放一首歌曲
    func playMusic(musicM: QQMusicModel) {
        
        // 给你一个数据模型, 播放 
        playTool.playMusic(musicM.filename)
        
        // 给当前播放索引赋值
        currentPlayIndex = playMusicList?.indexOf(musicM) ?? 0
        
    }
    
    // 继续播放当前歌曲
    
    func resumeCurrentMusic() -> () {
        playTool.resumeCurrentMusic()
    }
    
    // 暂停当前歌曲
    func pauseCurrentMusic() -> () {
        playTool.pauseCurrentMusic()
    }
    
    
    // 
    func seekToTime(time: NSTimeInterval) -> () {
        playTool.seekToTime(time)
    }
    
    
    
    // 设置锁屏界面信息
    func setUpLockMessage() -> () {
        
        
//        print("设置锁屏信息")

        let musicMM = getNewMusicMessage()
        
        // 1. 获取锁屏信息中心
        let center = MPNowPlayingInfoCenter.defaultCenter()
        
        
        // 1 获取当前正在播放的歌词文本
        let lrcName = musicMM.musicM?.lrcname ?? ""
        let lrcMs = QQMusicDataTool.getLrcMs(lrcName)
        let lrcMRow = QQMusicDataTool.getScrollRow(lrcMs, currentTime: musicMM.costTime)
        
        let lrcM = lrcMRow.lrcM
        let lrcStr = lrcM.lrcContent
        
       
       // 2 获取图片
        let image = UIImage(named: musicMM.musicM?.icon ?? "")
        
        
        // 3. 绘制文字到图片上, 生成一个新的图片
        if image != nil
        {
            if lastRow != lrcMRow.row {
//                print("test")
                lastRow = lrcMRow.row
                let resultImage = QQImageTool.createNewImage(image!, str: lrcStr)
                artwork = MPMediaItemArtwork(image: resultImage)
            }
            
            
        }
       
        
        
        
       
        
        
         // 2. 给信息中心赋值
         // 2.1 创建一个字典
        let name = musicMM.musicM?.name ?? ""
        let singer = musicMM.musicM?.singer ?? ""
        let totalTime =  musicMM.totalTime
        let costTime = musicMM.costTime
        
        var dic: [String: AnyObject] = [
            MPMediaItemPropertyAlbumTitle: name,
            MPMediaItemPropertyArtist: singer,
            MPMediaItemPropertyPlaybackDuration: totalTime,
            MPNowPlayingInfoPropertyElapsedPlaybackTime: costTime
        ]
        
        if artwork != nil {
            dic[MPMediaItemPropertyArtwork] = artwork!
        }
        
        // 2.2 给锁屏中心赋值
        center.nowPlayingInfo = dic
        
        // 3. 接收远程事件(如果不接收, 就不会显示锁屏界面)
        UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
        
    }
    
   
    
    
    
    
}
